package com.sgb.kjwl.view.ui.activity.transport;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.CompoundButton;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityMapGoodsBiddingBinding;
import com.sgb.kjwl.di.transport.DaggerTransportComponent;
import com.sgb.kjwl.di.transport.TransportModule;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.self.CarriageSelectEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.transport.MapGoodsBiddingEntity;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.view.CityPickBuilder;
import com.swgk.core.view.CityPickView;
import com.sgb.kjwl.view.widget.MapAdressDialog;
import com.sgb.kjwl.view.widget.MapGoodsBiddingPopWindow;
import com.sgb.kjwl.view.widget.MapPushPopWindow;
import com.sgb.kjwl.viewmodel.transport.MapGoodsBiddingModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

/**
 * 竞价找车发布页面
 */
public class MapGoodsBiddingActivity extends AppActivity implements ItemInputTextView.OnWholeItemClickListener {

    private ActivityMapGoodsBiddingBinding binding;
    private MapPushPopWindow pop;
    private int type = 1;//1 发布  2 编辑
    private String id = "";

    // 一级联动数据
    public static List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> addressEntityList;
    // 二级联动数据
    public static List<List<AddressEntity<List<AddressEntity>>>> opt2 = new ArrayList<>();
    // 三级联动数据
    public static List<List<List<AddressEntity>>> opt3 = new ArrayList<>();

    private int cityPosition1;
    private int cityPosition2;
    private int cityPosition3;
    private boolean isNewAddress;
    private InputMethodManager im;

    private Calendar selectedDate = Calendar.getInstance();

    private List<CarriageSelectEntity> isDumpList;
    private int isDumpPosition;

    private MapGoodsBiddingPopWindow pricePopWindow;

    @Inject
    MapGoodsBiddingModel sourceModel;

    int types=1;
    private String sendProId,sendCityId,sendAreaId,loadProId,loadCityId,loadAreaId;
    private Date today;

    public static void start(Context context){
        Intent intent = new Intent(context, MapGoodsBiddingActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerTransportComponent.builder().appComponent(appComponent).transportModule(new TransportModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_map_goods_bidding);
        binding.setViewModel(sourceModel);
        binding.setClickListener(this);
        binding.include.ivBack.setOnClickListener(this);
        binding.include.titleView.setText("竞价找车");

        binding.itemTvChengYun.setOnWholeItemClickListener(this);
        binding.itemTvIsSelfUnloading.setOnWholeItemClickListener(this);
        binding.llSendAdress.setOnClickListener(this);
        binding.ivAdress.setOnClickListener(this);
        binding.ivSend.setOnClickListener(this);
        binding.llAdress.setOnClickListener(this);
        binding.itemTvPricetime.setOnWholeItemClickListener(this);
        //底部选择弹窗
        pop = new MapPushPopWindow(this);
        pop.setBgView(binding.grayLayout);

        //货源选择弹窗
        pricePopWindow = new MapGoodsBiddingPopWindow(this);
        pricePopWindow.setBgView(binding.grayLayout);
        pricePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object goodsInfo, Object key2, Object key3) {
                sourceModel.sourceDetailEntity.set((MapGoodsBiddingEntity)goodsInfo);
                MapGoodsBiddingEntity entity = (MapGoodsBiddingEntity)goodsInfo;
                binding.itemTvChengYun.setContent(entity.getCityName() + " "+ entity.getDistrictName() + "->" + entity.getDestCityName() + " "+ entity.getDestDistrictName());

                if(entity.getIsDump().equals("1")){
                    binding.itemTvIsSelfUnloading.setContent("是");
                }else if(entity.getIsDump().equals("0")){
                    binding.itemTvIsSelfUnloading.setContent("否");
                }

                if(!TextUtils.isEmpty(entity.getShipmentTime())){
                    binding.selectLoadTime.setText(DateUtils.stampToDay(entity.getShipmentTime()));
                }

                if(!TextUtils.isEmpty(entity.getShipmentEndTime())){
                    binding.selectLoadEndTime.setText(DateUtils.stampToDay(entity.getShipmentEndTime()));
                }
            }
        });

        im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

        int beforeDot = 3;
        int afterDot = 2;
        binding.selectGoodsWeight.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void afterTextChanged(Editable editable) {
                String temp = editable.toString();
                int posDot = temp.indexOf(".");
                //直接输入小数点的情况
                if (posDot == 0) {
                    editable.insert(0, "0");
                    return;
                }
                //连续输入0
                if (temp.equals("00")) {
                    editable.delete(1, 2);
                    return;
                }
                //输入"08" 等类似情况
                if (temp.startsWith("0") && temp.length() > 1 && (posDot == -1 || posDot > 1)) {
                    editable.delete(0, 1);
                    return;
                }

                //不包含小数点 不限制小数点前位数
                if (posDot < 0 && beforeDot == -1) {
                    //do nothing 仅仅为了理解逻辑而已
                    return;
                } else if (posDot < 0 && beforeDot != -1) {
                    //不包含小数点 限制小数点前位数
                    if (temp.length() <= beforeDot) {
                        //do nothing 仅仅为了理解逻辑而已
                    } else {
                        editable.delete(beforeDot, beforeDot + 1);
                    }
                    return;
                }

                //如果包含小数点 限制小数点后位数
                if (temp.length() - posDot - 1 > afterDot && afterDot != -1) {
                    editable.delete(posDot + afterDot + 1, posDot + afterDot + 2);//删除小数点后多余位数
                }
            }
        });
        binding.ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    binding.ck2.setChecked(false);
                    sourceModel.sourceDetailEntity.get().setPattern(0);
                    binding.itemTvPricetime.setVisibility(View.GONE);
                }
            }
        });
        binding.ck2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    binding.ck1.setChecked(false);
                    sourceModel.sourceDetailEntity.get().setPattern(1);
                    binding.itemTvPricetime.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if(null!=im){
            im.hideSoftInputFromWindow(binding.publicSure.getWindowToken(),0);
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void init() {

        sourceModel.sourceList();
        sourceModel.getUserInfo();
        sourceModel.getAddressModel();
        sourceModel.getGoodsType();
        sourceModel.getCarsType();
        sourceModel.getCarsLength();
        sourceModel.getHandleType();
        sourceModel.getInvoiceType();
        sourceModel.getPayType();
        sourceModel.getTransType();
    }

    public void showSourceList(List<MapGoodsBiddingEntity> list){
        if(null==list || list.size() == 0){
            MToast.showToast(MapGoodsBiddingActivity.this,"暂无数据");
            return;
        }
        pricePopWindow.setDatas(list);
    }

    public void displayRole(UserMsgEntity data) {
        if(null!=data){
            binding.person.setText(data.getRealName());
            binding.phone.setText(data.getTelPhone());
            binding.selectGoodsCompany.setText(data.getUsername());
        }
    }

    public void initAddressData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> datas) {
        addressEntityList = datas;
        initJsonData();
    }

    private void showCityPop(String title) {
        CityPickView cityOption = new CityPickBuilder(MapGoodsBiddingActivity.this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                String areaName = "";
                if (opt3.get(options1).get(options2).get(options3) == null) {
                    areaName = "";
                } else {
                    areaName = opt3.get(options1).get(options2).get(options3).getName();
//                    mAreaCode = opt3.get(options1).get(options2).get(options3).getNo();
                    cityPosition3 = options3;
                }
                String content = addressEntityList.get(options1).getName() + opt2.get(options1).get(options2).getName() + areaName;
                cityPosition1 = options1;
                cityPosition2 = options2;

                if(title.equals("请选择发货地")){
                    if(!TextUtils.isEmpty(areaName)){
                        sourceModel.sourceDetailEntity.get().setDistrictName(areaName);
                        sourceModel.sourceDetailEntity.get().setDistrictNo(opt3.get(options1).get(options2).get(options3).getNo());
                    }
                    sourceModel.sourceDetailEntity.get().setProvinceName(addressEntityList.get(options1).getName());
                    sourceModel.sourceDetailEntity.get().setProvinceNo(addressEntityList.get(options1).getId());
                    sourceModel.sourceDetailEntity.get().setCityName(opt2.get(options1).get(options2).getName());
                    sourceModel.sourceDetailEntity.get().setCityNo(opt2.get(options1).get(options2).getNo());
                    sendProId=addressEntityList.get(options1).getId();
                    sendCityId=opt2.get(options1).get(options2).getNo();
                    sendAreaId=opt3.get(options1).get(options2).get(options3).getNo();
                    binding.selectSendLocation.setText(content);
                }else if(title.equals("请选择目的地")){
                    if(!TextUtils.isEmpty(areaName)){
                        sourceModel.sourceDetailEntity.get().setDestDistrictName(areaName);
                        sourceModel.sourceDetailEntity.get().setDestDistrictNo(opt3.get(options1).get(options2).get(options3).getNo());
                    }
                    sourceModel.sourceDetailEntity.get().setDestProvinceName(addressEntityList.get(options1).getName());
                    sourceModel.sourceDetailEntity.get().setDestProvinceNo(addressEntityList.get(options1).getId());
                    sourceModel.sourceDetailEntity.get().setDestCityName(opt2.get(options1).get(options2).getName());
                    sourceModel.sourceDetailEntity.get().setDestCityNo(opt2.get(options1).get(options2).getNo());
                    loadProId=addressEntityList.get(options1).getId();
                    loadCityId=opt2.get(options1).get(options2).getNo();
                    loadAreaId=opt3.get(options1).get(options2).get(options3).getNo();
                    binding.selectPurposeLocation.setText(content);
                }else if(title.equals("请选择范围")){
                    if(!TextUtils.isEmpty(areaName)){
                        sourceModel.sourceDetailEntity.get().setPushDistrictName(areaName);
                        sourceModel.sourceDetailEntity.get().setPushDistrictNo(opt3.get(options1).get(options2).get(options3).getNo());
                    }
                    sourceModel.sourceDetailEntity.get().setPushProvinceName(addressEntityList.get(options1).getName());
                    sourceModel.sourceDetailEntity.get().setPushProvinceNo(addressEntityList.get(options1).getId());
                    sourceModel.sourceDetailEntity.get().setPushCityName(opt2.get(options1).get(options2).getName());
                    sourceModel.sourceDetailEntity.get().setPushCityNo(opt2.get(options1).get(options2).getNo());

//                    binding.rangeSelect.setText(content);
                }
            }
        }).setCancelText("取消").setSubCalSize(15)
                .setCancelColor(Color.parseColor("#666666"))
                .setTitleText(title)
                .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                .setSubmitText("确定")
                .setSubmitColor(Color.parseColor("#5792FD"))
                .build();
        cityOption.setPicker(addressEntityList, opt2, opt3);
        cityOption.setSelectOptions(cityPosition1, cityPosition2, cityPosition3);
        cityOption.show();
    }

    private void initJsonData() {

        for (int i = 0; i < addressEntityList.size(); i++) {

//            cityList = new ArrayList<>();
            ArrayList areaList = new ArrayList<>();
            opt2.add(addressEntityList.get(i).getChildren());
            for (int j = 0; j < addressEntityList.get(i).getChildren().size(); j++) {

                List<AddressEntity> cityAreaList = new ArrayList<>();
                List<AddressEntity> children = addressEntityList.get(i).getChildren().get(j).getChildren();
                if (children == null || children.size() == 0) {
                    cityAreaList.add(null);
                } else {
                    cityAreaList.addAll(children);
                }
                areaList.add(cityAreaList);
            }
            opt3.add(areaList);
        }

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

        switch (v.getId()){
            case R.id.public_sure:
                pushData();
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.select_goods_type:
                if(null!=sourceModel.sourcesType && sourceModel.sourcesType.size()>0){
                    pop.setData("选择货物类型",sourceModel.sourcesType);
                    pop.setOnResultCallBack(new BasePopWindow.OnResultCallBack() {
                        @Override
                        public void result(Object result) {
                            Log.v("selectggg",((GoodsInfoTypeEntity3)result).getName());
                            GoodsInfoTypeEntity3 entity = (GoodsInfoTypeEntity3)result;
                            if(null!=entity){
                                binding.selectGoodsType.setText(entity.getName());
                                sourceModel.sourceDetailEntity.get().setGoodsModelName(entity.getName());
                                sourceModel.sourceDetailEntity.get().setGoodsModeNo(entity.getNo());
                            }
                        }
                    });
                    pop.showDownPopwindow(binding.grayLayout, true);
                }else{
                    MToast.showToast(getActivity(),"数据未加载");
                }
                break;
            case R.id.select_car_type:
                if(null!=sourceModel.carsType && sourceModel.carsType.size()>0){
                    pop.setData("选择车型",sourceModel.carsType);
                    pop.setOnResultCallBack(new BasePopWindow.OnResultCallBack() {
                        @Override
                        public void result(Object result) {
                            Log.v("selectsss",((GoodsInfoTypeEntity3)result).getName());
                            GoodsInfoTypeEntity3 entity = (GoodsInfoTypeEntity3)result;
                            if(null!=entity){
                                binding.selectCarType.setText(entity.getName());
                                sourceModel.sourceDetailEntity.get().setCarModelName(entity.getName());
                                sourceModel.sourceDetailEntity.get().setCarModelNo(entity.getNo());
                            }
                        }
                    });
                    pop.showDownPopwindow(binding.grayLayout, true);
                }else{
                    MToast.showToast(getActivity(),"数据未加载");
                }
                break;
            case R.id.select_car_length:
                if(null!=sourceModel.carsLengthType && sourceModel.carsLengthType.size()>0){
                    pop.setData("选择车长",sourceModel.carsLengthType);
                    pop.setOnResultCallBack(new BasePopWindow.OnResultCallBack() {
                        @Override
                        public void result(Object result) {
                            Log.v("selectttt",((GoodsInfoTypeEntity3)result).getName());
                            GoodsInfoTypeEntity3 entity = (GoodsInfoTypeEntity3)result;
                            if(null!=entity){
                                binding.selectCarLength.setText(entity.getName());
                                sourceModel.sourceDetailEntity.get().setCarLenName(entity.getName());
                                sourceModel.sourceDetailEntity.get().setCarLenNo(entity.getNo());
                            }
                        }
                    });
                    pop.showDownPopwindow(binding.grayLayout, true);
                }else{
                    MToast.showToast(getActivity(),"数据未加载");
                }
                break;
            case R.id.select_transport_request:
                if(null!=sourceModel.transportType && sourceModel.transportType.size()>0){
                    pop.setData("选择运输类型",sourceModel.transportType);
                    pop.setOnResultCallBack(new BasePopWindow.OnResultCallBack() {
                        @Override
                        public void result(Object result) {
                            Log.v("selectaaaa",((GoodsInfoTypeEntity3)result).getName());
                            GoodsInfoTypeEntity3 entity = (GoodsInfoTypeEntity3)result;
                            if(null!=entity){
                                binding.selectTransportRequest.setText(entity.getName());
                                sourceModel.sourceDetailEntity.get().setTransportTypeName(entity.getName());
                                sourceModel.sourceDetailEntity.get().setTransportType(entity.getNo());
                            }
                        }
                    });
                    pop.showDownPopwindow(binding.grayLayout, true);
                }else{
                    MToast.showToast(getActivity(),"数据未加载");
                }
                break;
            case R.id.select_pay_type:
                if(null!=sourceModel.payType && sourceModel.payType.size()>0){
                    pop.setData("选择支付方式",sourceModel.payType);
                    pop.setOnResultCallBack(new BasePopWindow.OnResultCallBack() {
                        @Override
                        public void result(Object result) {
                            Log.v("selectaaaa",((GoodsInfoTypeEntity3)result).getName());
                            GoodsInfoTypeEntity3 entity = (GoodsInfoTypeEntity3)result;
                            if(null!=entity){
                                binding.selectPayType.setText(entity.getName());
                                sourceModel.sourceDetailEntity.get().setPayTypeName(entity.getName());
                                sourceModel.sourceDetailEntity.get().setPayType(entity.getName());
                            }
                        }
                    });
                    pop.showDownPopwindow(binding.grayLayout, true);
                }else{
                    MToast.showToast(getActivity(),"数据未加载");
                }
                break;
            case R.id.select_unload_type:
                if(null!=sourceModel.handlingType && sourceModel.handlingType.size()>0){
                    pop.setData("选择装卸方式",sourceModel.handlingType);
                    pop.setOnResultCallBack(new BasePopWindow.OnResultCallBack() {
                        @Override
                        public void result(Object result) {
                            Log.v("selectaaaa",((GoodsInfoTypeEntity3)result).getName());
                            GoodsInfoTypeEntity3 entity = (GoodsInfoTypeEntity3)result;
                            if(null!=entity){
                                binding.selectUnloadType.setText(entity.getName());
                                sourceModel.sourceDetailEntity.get().setHandleTypeName(entity.getName());
                                sourceModel.sourceDetailEntity.get().setHandleTypeNo(entity.getNo());
                            }
                        }
                    });
                    pop.showDownPopwindow(binding.grayLayout, true);
                }else{
                    MToast.showToast(getActivity(),"数据未加载");
                }
                break;
            case R.id.select_invoice_request:
                if(null!=sourceModel.invoiceType && sourceModel.invoiceType.size()>0){
                    pop.setData("选择发票方式",sourceModel.invoiceType);
                    pop.setOnResultCallBack(new BasePopWindow.OnResultCallBack() {
                        @Override
                        public void result(Object result) {
                            Log.v("selectaaaa",((GoodsInfoTypeEntity3)result).getName());
                            GoodsInfoTypeEntity3 entity = (GoodsInfoTypeEntity3)result;
                            if(null!=entity){
                                binding.selectInvoiceRequest.setText(entity.getName());
                                sourceModel.sourceDetailEntity.get().setInvoiceName(entity.getName());
                                sourceModel.sourceDetailEntity.get().setInvoice(entity.getNo());
                            }
                        }
                    });
                    pop.showDownPopwindow(binding.grayLayout, true);
                }else{
                    MToast.showToast(getActivity(),"数据未加载");
                }
                break;
            case R.id.select_send_location:
                if(null!=addressEntityList && addressEntityList.size()>0){
                    showCityPop("请选择发货地");
                }else{
                    MToast.showToast(getActivity(),"数据未加载");
                }
                break;
            case R.id.select_purpose_location:
                if(null!=addressEntityList && addressEntityList.size()>0){
                    showCityPop("请选择目的地");
                }else{
                    MToast.showToast(getActivity(),"数据未加载");
                }
                break;
            case R.id.range_select:
                if(null!=addressEntityList && addressEntityList.size()>0){
                    showCityPop("请选择范围");
                }else{
                    MToast.showToast(getActivity(),"数据未加载");
                }
                break;
            case R.id.select_load_time:
                showTimePop(0);
                break;
            case R.id.select_load_end_time:
                showTimePop(1);
                break;
            case R.id.iv_send:
            case R.id.ll_send_adress:
                types=1;
                if (!TextUtils.isEmpty(sendProId)) {
                    //跳转详细地址界面
                    sourceModel.getLocationLL(sendProId,sendCityId, sendAreaId, 1);
                } else {
                    toMap(0,0);
                }
                break;
            case R.id.iv_adress:
            case R.id.ll_address:
                types=2;
                if (!TextUtils.isEmpty(loadProId)) {
                    //跳转详细地址界面
                    sourceModel.getLocationLL(loadProId,loadCityId, loadAreaId, 1);
                } else {
                    toMap(0,0);
                }
                break;


        }
    }
    //  返回经纬度信息
    public void getLocationLL(MapLongAndLatEntity data, int num) {

        if (!TextUtils.isEmpty(data.getLat())) {
            if (!TextUtils.isEmpty(data.getLng())) {

                toMap(Double.parseDouble(data.getLat()),Double.parseDouble(data.getLng()));

            }
        }
    }
    public void pushData(){

        if (TextUtils.isEmpty(binding.selectGoodsCompany.getText().toString())) {
            MToast.showToast(MapGoodsBiddingActivity.this, "请输入发货单位");
            return;
        } else if (TextUtils.isEmpty(binding.selectGoodsType.getText().toString())) {
            MToast.showToast(MapGoodsBiddingActivity.this, "请选择货物类型");
            return;
        } else if (TextUtils.isEmpty(binding.selectSendLocation.getText().toString())) {
            MToast.showToast(MapGoodsBiddingActivity.this, "请选择发货地");
            return;
        } else if (TextUtils.isEmpty(binding.selectPurposeLocation.getText().toString())) {
            MToast.showToast(MapGoodsBiddingActivity.this, "请选择目的地");
            return;
        } else if (TextUtils.isEmpty(binding.selectGoodsWeight.getText().toString())||binding.selectGoodsWeight.equals("0")) {
            MToast.showToast(MapGoodsBiddingActivity.this, "请输入重量");
            return;
        } else if (TextUtils.isEmpty(binding.selectPrice.getText().toString())) {
            MToast.showToast(MapGoodsBiddingActivity.this, "请输入运费单价");
            return;
        } else if (TextUtils.isEmpty(binding.selectLoadTime.getText().toString())){
            MToast.showToast(MapGoodsBiddingActivity.this, "请选择可装货开始时间");
            return;
        }  else if (TextUtils.isEmpty(binding.selectLoadEndTime.getText().toString())){
            MToast.showToast(MapGoodsBiddingActivity.this, "请选择可装货结束时间");
            return;
        }/* else if (TextUtils.isEmpty(binding.selectUnloadType.getText().toString())) {
            MToast.showToast(MapGoodsBiddingActivity.this, "请选择装卸方式");
            return;
        }*/ /*else if (TextUtils.isEmpty(binding.selectLoadLocation.getText().toString())) {
            MToast.showToast(MapGoodsBiddingActivity.this, "请输入装货地点");
            return;
        }*/ else if (TextUtils.isEmpty(binding.selectCarType.getText().toString())) {
            MToast.showToast(MapGoodsBiddingActivity.this, "请选择车型");
            return;
        } else if (TextUtils.isEmpty(binding.selectCarHeight.getText().toString())) {
            MToast.showToast(MapGoodsBiddingActivity.this, "请输入栏高");
            return;
        } else if (TextUtils.isEmpty(binding.selectCarLength.getText().toString())) {
            MToast.showToast(MapGoodsBiddingActivity.this, "请选择车长");
            return;
        } else if (TextUtils.isEmpty(binding.itemTvIsSelfUnloading.getContent())) {
            MToast.showToast(MapGoodsBiddingActivity.this, "请选择自卸方式");
            return;
        } else if (TextUtils.isEmpty(binding.selectPayType.getText().toString())) {
            MToast.showToast(MapGoodsBiddingActivity.this, "请选择付款方式");
            return;
        } /*else if (TextUtils.isEmpty(binding.selectQualiRequest.getText().toString())) {
            MToast.showToast(MapGoodsBiddingActivity.this, "请输入资质要求");
            return;
        } */else if (TextUtils.isEmpty(binding.selectInvoiceRequest.getText().toString())) {
            MToast.showToast(MapGoodsBiddingActivity.this, "请选择发票要求");
            return;
        } /*else if (TextUtils.isEmpty(binding.selectTransportRequest.getText().toString())) {
            MToast.showToast(MapGoodsBiddingActivity.this, "请选择运输要求");
            return;
        }*/
        else if(sourceModel.sourceDetailEntity.get().getPattern()==1&&TextUtils.isEmpty(binding.itemTvPricetime.getContent())){

                MToast.showToast(MapGoodsBiddingActivity.this, "请输入截止时间");
                return;

        }else if (TextUtils.isEmpty(binding.person.getText().toString())) {
            MToast.showToast(MapGoodsBiddingActivity.this, "请输入联系人");
            return;
        } else  if (TextUtils.isEmpty(binding.phone.getText().toString())) {
            MToast.showToast(MapGoodsBiddingActivity.this, "请输入手机号");
            return;
        }

        sourceModel.sourceDetailEntity.get().setCompanyName(binding.selectGoodsCompany.getText().toString());
        sourceModel.sourceDetailEntity.get().setHeightHurdle(binding.selectCarHeight.getText().toString());
        sourceModel.sourceDetailEntity.get().setFreight(binding.selectPrice.getText().toString());
        sourceModel.sourceDetailEntity.get().setWeight(binding.selectGoodsWeight.getText().toString());
        sourceModel.sourceDetailEntity.get().setDetailAddress(binding.tvSendAdress.getText().toString());
        sourceModel.sourceDetailEntity.get().setDestDetailAddress(binding.tvAdress.getText().toString());
        sourceModel.sourceDetailEntity.get().setContrcts(binding.person.getText().toString());
        sourceModel.sourceDetailEntity.get().setContrctsNumber(binding.phone.getText().toString());

        //备注
        sourceModel.sourceDetailEntity.get().setNote(binding.marks.getText().toString());
        sourceModel.publicSource();
    }

    @Override
    public void OnWholeItemClick(View view) {
        switch (view.getId()) {
            case R.id.item_tv_cheng_yun:
                pricePopWindow.showPopwindowBottom(binding.publicSure);
                break;
            case R.id.item_tv_is_self_unloading:
                if (isDumpList != null) {
                    showIsDumpPop();
                } else {
                    sourceModel.isDump();
                }
                break;
            case R.id.item_tv_pricetime:
                TimePickerView timeEndPicker = new TimePickerBuilder(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {

                        Calendar cal1 = Calendar.getInstance();
                        cal1.setTime(date);
                        cal1.set(Calendar.HOUR_OF_DAY, 23);
                        cal1.set(Calendar.MINUTE, 59);
                        cal1.set(Calendar.SECOND, 59);
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        String format = sdf.format(date);

                        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        binding.itemTvPricetime.setContent(format);
                        sourceModel.sourceDetailEntity.get().setEndTime(DateUtils.dateTo10Timestamp3(sdf1.format(cal1.getTime()))+"");

                    }
                }).setRangDate(Calendar.getInstance(),null).build();
                timeEndPicker.show();
                break;
        }
    }

    private void showTimePop(int type){

        TimePickerView timePicker = new TimePickerBuilder(MapGoodsBiddingActivity.this, (date, v) -> {
            @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(date);

            try {
                if(type ==0 ){

                    binding.selectLoadTime.setText(format);
                    sourceModel.sourceDetailEntity.get().setShipmentTime(DateUtils.dateTo13Timestamp(format)+"");
                }else if(type == 1){

                    binding.selectLoadEndTime.setText(format);
                    sourceModel.sourceDetailEntity.get().setShipmentEndTime(DateUtils.dateTo13Timestamp(format)+"");
                }
                selectedDate.setTime(date);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).setType(new boolean[]{true, true, true, false, false, false})
                .setCancelText("取消").setSubCalSize(15)
                .setCancelColor(Color.parseColor("#666666"))
                .setTitleText("请选择装货时间")
                .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                .setSubmitText("确定")
                .setSubmitColor(Color.parseColor("#5792FD"))
                .setRangDate(Calendar.getInstance(),null)
                .build();
        timePicker.show();
        timePicker.setDate(selectedDate);

    }

    //是否自卸
    public void showIsDumpPop(List<CarriageSelectEntity> isDumpList) {
        this.isDumpList = isDumpList;
        showIsDumpPop();
    }

    private void showIsDumpPop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(MapGoodsBiddingActivity.this, (options1, options2, options3, v) -> {
            String content = isDumpList.get(options1).getName();
            binding.itemTvIsSelfUnloading.setContent(content);
            isDumpPosition = options1;
            sourceModel.sourceDetailEntity.get().setIsDump(isDumpList.get(options1).getNo());
        }).build();
        businessOption.setPicker(isDumpList);
        businessOption.setSelectOptions(isDumpPosition);
        businessOption.show();
    }

    public void toMap(double la,double lo){
        MapAdressDialog dialog= new MapAdressDialog(getActivity(),null);
        dialog.setLocation(la,lo);
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogs) {
                dialog.destoryMap();
            }
        });
        dialog.setDialogClickCallBack(new MapAdressDialog.DialogClickCallBack() {
            @Override
            public void onClickMarker(String adress, String pro, String city, String area, double latitude, double longtitude) {
                if(types==1) {
                    binding.tvSendAdress.setText(adress);
                    sourceModel.sourceDetailEntity.get().setDetailAddress(adress);
                    sourceModel.sourceDetailEntity.get().setLatitude(latitude);
                    sourceModel.sourceDetailEntity.get().setLongitude(longtitude);
                    sourceModel.sourceDetailEntity.get().setProvinceName(pro);
                    sourceModel.sourceDetailEntity.get().setCityName(city);
                    sourceModel.sourceDetailEntity.get().setDistrictName(area);
                    binding.selectSendLocation.setText(pro + city + area);
                }else{
                    binding.tvAdress.setText(adress);
                    binding.selectPurposeLocation.setText(pro + city + area);
                    sourceModel.sourceDetailEntity.get().setDestLatitude(latitude);
                    sourceModel.sourceDetailEntity.get().setDestLongitude(longtitude);
                    sourceModel.sourceDetailEntity.get().setDestDetailAddress(adress);
                    sourceModel.sourceDetailEntity.get().setDestProvinceName(pro);
                    sourceModel.sourceDetailEntity.get().setDestCityName(city);
                    sourceModel.sourceDetailEntity.get().setDestDistrictName(area);
                }
                for(AddressEntity entity:addressEntityList){
                    if(entity.getName().equals(pro)){
                        MLog.d("locationOffOder","proON "+entity.getNo()+"  "+entity.getName());
                        if(types==1) {
                            sourceModel.sourceDetailEntity.get().setProvinceNo(entity.getNo());
                        }else{
                            sourceModel.sourceDetailEntity.get().setDestProvinceNo(entity.getNo());
                        }
                        break;
                    }

                }
                boolean isbreak1=false;

                for (int i=0;i<opt2.size();i++){
                    for(int j=0;j<opt2.get(i).size();j++) {
                        if (opt2.get(i).get(j).getName().equals(city)) {
                            MLog.d("locationOffOder", "cityON1 " + opt2.get(i).get(j).getNo() + "  ");
                            if(types==1) {
                                sourceModel.sourceDetailEntity.get().setCityNo(opt2.get(i).get(j).getNo());
                            }else{
                                sourceModel.sourceDetailEntity.get().setDestCityNo(opt2.get(i).get(j).getNo());
                            }
                            isbreak1=true;
                            break;
                        }
                    }
                    if(isbreak1){
                        break;
                    }

                }
                boolean isbreak2=false;
                for (int i=0;i<opt3.size();i++){
                    for(int j=0;j<opt3.get(i).size();j++) {
                        for(int n=0;n<opt3.get(i).get(j).size();n++){
                            if(opt3.get(i).get(j).get(n)!=null) {
                                MLog.d("locationOffOder", "Direction " + opt3.get(i).get(j).get(n).getName() + "  ");
                                if (opt3.get(i).get(j).get(n).getName().equals(area)) {
                                    MLog.d("locationOffOder", "cityON2 " + opt3.get(i).get(j).get(n).getNo() + "  ");
                                    if(types==1) {
                                        sourceModel.sourceDetailEntity.get().setDistrictNo(opt3.get(i).get(j).get(n).getNo());
                                    }else{
                                        sourceModel.sourceDetailEntity.get().setDestDistrictNo(opt3.get(i).get(j).get(n).getNo());
                                    }
                                    isbreak2=true;
                                    break;
                                }
                            }
                        }
                        if(isbreak2){
                            break;
                        }
                    }
                    if(isbreak2){
                        break;
                    }
                }
            }
        });
        dialog.show();
    }
}
